package com.example.health_life.controller;

import com.example.health_life.Response;
import com.example.health_life.dao.Orderitem;
import com.example.health_life.service.OrderitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orderitems")
public class OrderitemController {

    @Autowired
    private OrderitemService orderitemService;

    @GetMapping
    public Response<List<Orderitem>> getOrderitemList(@RequestParam(required = false) Integer orderId) {
        List<Orderitem> orderitems = orderitemService.getOrderitemList(orderId);
        return Response.newSuccess(orderitems);
    }

    @GetMapping("/{id}")
    public Response<Orderitem> getOrderitemById(@PathVariable Integer id) {
        Orderitem orderitem = orderitemService.getOrderitemById(id);
        return Response.newSuccess(orderitem);
    }

    @PostMapping
    public Response<Integer> addOrderitem(@RequestBody Orderitem orderitem) {
        int id = orderitemService.addOrderitem(orderitem);
        return Response.newSuccess(id);
    }

    @PutMapping("/{id}")
    public Response<Integer> updateOrderitem(@PathVariable Integer id,
                                           @RequestParam(required = false) Integer quantity,
                                           @RequestParam(required = false) Double price,
                                           @RequestParam(required = false) Integer productId) {
        int result = orderitemService.updateOrderitem(id, quantity, price, productId);
        return Response.newSuccess(result);
    }

    @DeleteMapping("/{id}")
    public Response<Integer> deleteOrderitem(@PathVariable Integer id) {
        int result = orderitemService.deleteOrderitem(id);
        return Response.newSuccess(result);
    }

    @DeleteMapping("/order/{orderId}")
    public Response<Integer> deleteOrderitemsByOrderId(@PathVariable Integer orderId) {
        int result = orderitemService.deleteOrderitemsByOrderId(orderId);
        return Response.newSuccess(result);
    }
}
